package com.huawei.smartcity0102.home;

import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.huawei.smartcity0102.Base;
import com.huawei.smartcity0102.MainActivity;
import com.huawei.smartcity0102.R;
import com.huawei.smartcity0102.model.all;
import com.huawei.smartcity0102.model.banner;
import com.huawei.smartcity0102.model.newlist;
import com.huawei.smartcity0102.model.newtype;
import com.huawei.smartcity0102.news.F_News;
import com.huawei.smartcity0102.news.F_News1;
import com.huawei.smartcity0102.util.Ad;
import com.huawei.smartcity0102.util.BannerAd;
import com.huawei.smartcity0102.util.Http;
import com.huawei.smartcity0102.util.XC;
import com.huawei.smartcity0102.wm.F_W;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZengYunyi
 * @description:
 * @date :2022/1/2 13:54
 */
public class F_Home extends Base {
    private SearchView search;
    private Banner banner;
    private RecyclerView rv;
    private RecyclerView rv1;
    private TabLayout tab;
    private RecyclerView rv2;

    @Override
    protected int layId() {
        return R.layout.f_home;
    }

    @Override
    protected String title() {
        return "首页";
    }

    @Override
    protected Boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return true;
    }

    List<all.RowsDTO> alist;
    List<banner.RowsDTO> blist;
    List<newlist.RowsDTO> nlist;
    List<newtype.DataDTO> tlist;
    public static newlist.RowsDTO news;
    public static List<newlist.RowsDTO> newlist=new ArrayList<>();
    @Override
    protected void init(View iv) {
        initView(iv);
        XC xc = new XC(4);
        xc.then(() -> {
            alist = Http.getInstance().get("/prod-api/api/service/list", null, all.class, 1).getRows().subList(0, 9);
            xc.adds();
        });
        xc.then(() -> {
            blist = Http.getInstance().get("/prod-api/api/rotation/list?type=2", null, banner.class, 1).getRows();
            xc.adds();
        });
        xc.then(() -> {
            nlist = Http.getInstance().get("/prod-api/press/press/list", null, newlist.class, 1).getRows();
            xc.adds();
        });
        xc.then(() -> {
            tlist = Http.getInstance().get("/prod-api/press/category/list", null, newtype.class, 1).getData();
            xc.adds();
        });
        xc.livedata.observe(getViewLifecycleOwner(), a1 -> {
            if (a1.get() == 4) {
                List<Object> list = new ArrayList<>();
                for (com.huawei.smartcity0102.model.banner.RowsDTO b : blist) {
                    list.add(b.getAdvImg());
                }
                banner.setAdapter(new BannerAd(list)).addBannerLifecycleObserver(this).setIndicator(new CircleIndicator(getContext())).start();
                banner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(Object o, int i) {
                        newlist.clear();
                        newlist.addAll(nlist.stream().filter(a->a.getId()== (int) blist.get(i).getTargetId()).collect(Collectors.toList()));
                        add(new F_News());
                    }
                });
                all.RowsDTO all = new all.RowsDTO();
                all.setServiceName("更多服务");
                alist.add(all);
                Ad<all.RowsDTO> ad = new Ad<all.RowsDTO>(R.layout.r_all, alist) {
                    private void initView(View iv) {
                        ig = (ImageView) iv.findViewById(R.id.ig);
                        tv = (TextView) iv.findViewById(R.id.tv);
                    }

                    private TextView tv;
                    private ImageView ig;

                    @Override
                    protected void initD(View vi, all.RowsDTO data, int p) {
                        initView(vi);
                        if (data.getServiceName().equals("更多服务")) {
                            Glide.with(ig.getContext()).load(R.drawable.more).into(ig);
                        } else {
                            Glide.with(ig.getContext()).load(Http.base + data.getImgUrl()).into(ig);
                        }
                        tv.setText(data.getServiceName());
                        vi.setOnClickListener(v->{

                            adds(data.getServiceName());
                        });
                    }
                };
                List<newlist.RowsDTO> list1 = new ArrayList<>();
                list1.addAll(nlist.subList(3, 5));
                Ad<newlist.RowsDTO> ad1 = new Ad<newlist.RowsDTO>(R.layout.r_rm, list1) {
                    private void initView(View iv) {
                        ig = (ImageView) iv.findViewById(R.id.ig);
                        tv = (TextView) iv.findViewById(R.id.tv);
                    }

                    private TextView tv;
                    private ImageView ig;

                    @Override
                    protected void initD(View vi, newlist.RowsDTO data, int p) {
                        initView(vi);
                        Glide.with(ig.getContext()).load(Http.base + data.getCover()).into(ig);
                        tv.setText(data.getTitle());
                        vi.setOnClickListener(v -> {
                            news=data;
                            add(new F_News1());
                        });
                    }
                };
                tab.removeAllTabs();
                for (newtype.DataDTO t : tlist) {
                    tab.addTab(tab.newTab().setText(t.getName()));
                }
                List<newlist.RowsDTO> list2 = new ArrayList<>();
                list2.addAll(nlist.stream().filter(a -> Integer.parseInt(a.getType()) == tlist.get(0).getId()).collect(Collectors.toList()));
                Ad<newlist.RowsDTO> ad3 = new Ad<newlist.RowsDTO>(R.layout.r_base, list2) {
                    private void initView(View iv) {
                        ig = (ImageView) iv.findViewById(R.id.ig);
                        tv1 = (TextView) iv.findViewById(R.id.tv1);
                        tv2 = (TextView) iv.findViewById(R.id.tv2);
                        tv3 = (TextView) iv.findViewById(R.id.tv3);
                        tv4 = (TextView) iv.findViewById(R.id.tv4);
                        tv5 = (TextView) iv.findViewById(R.id.tv5);
                        web = (WebView) iv.findViewById(R.id.web);
                    }

                    private WebView web;
                    private TextView tv5;
                    private TextView tv4;
                    private TextView tv3;
                    private TextView tv2;
                    private TextView tv1;
                    private ImageView ig;

                    @Override
                    protected void initD(View vi, newlist.RowsDTO data, int p) {
                        initView(vi);
                        Glide.with(ig.getContext()).load(Http.base + data.getCover()).into(ig);
                        tv1.setText(data.getTitle());
                        tv2.setVisibility(View.GONE);
                        tv5.setVisibility(View.GONE);
                        tv3.setText("评论总数"+data.getTitle());
                        web.setVisibility(View.VISIBLE);
                        tv4.setText("发布时间"+data.getCreateTime());
                        web.loadDataWithBaseURL(Http.base,data.getContent(),"text/html","utf-8",null);
                        vi.setOnClickListener(v->{
                            news=data;
                            add(new F_News1());
                        });
                    }
                };
                search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        newlist.clear();
                        newlist.addAll(nlist.stream().filter(a->a.getTitle().contains(query)).collect(Collectors.toList()));
                        add(new F_News());
                        return true;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });
                tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        list2.clear();
                        list2.addAll(nlist.stream().filter(a -> Integer.parseInt(a.getType()) == tlist.stream().filter(b->b.getName().equals(tab.getText())).findFirst().get().getId()).collect(Collectors.toList()));
                        ad3.notifyDataSetChanged();
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
                rv.setAdapter(ad);
                rv.setLayoutManager(new GridLayoutManager(getContext(),5));
                rv1.setAdapter(ad1);
                rv1.setLayoutManager(new GridLayoutManager(getContext(),2));
                rv2.setAdapter(ad3);
                rv2.setLayoutManager(new LinearLayoutManager(getContext()));
            }
        });
    }

    private void adds(String serviceName) {
        switch (serviceName) {
            case "更多服务":
                MainActivity.bottom.setSelectedItemId(R.id.all);
                break;
            case "外卖订餐":
                add(new F_W());
                break;
        }
    }

    private void initView(View iv) {
        search = (SearchView) iv.findViewById(R.id.search);
        banner = (Banner) iv.findViewById(R.id.banner);
        rv = (RecyclerView) iv.findViewById(R.id.rv);
        rv1 = (RecyclerView) iv.findViewById(R.id.rv1);
        tab = (TabLayout) iv.findViewById(R.id.tab);
        rv2 = (RecyclerView) iv.findViewById(R.id.rv2);
    }
}
